package com.xuyuan.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xuyuan.entity.MyTestEntity;
import com.xuyuan.service.MyService;
import com.xuyuan.spring.async.AsyncService;
import com.xuyuan.spring.event.CustomMyEvent;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/xuyuan1")
public class MyController {

    @Autowired
    private MyService service;

    @Autowired
    private ApplicationEventPublisher publisher;

    @Autowired
    private ObjectMapper mapper;

    @Resource
    AsyncService asyncService;

    //@RequestMapping(method = RequestMethod.POST, value = "/test")
    @PostMapping("/test")
    public String test1() {
        return service.getName(Boolean.TRUE);
    }

    @PostMapping("/register")
    public String register(@RequestParam("email") String email) {
        ObjectNode objectNode = mapper.createObjectNode();
        objectNode.put("email", email);
        objectNode.put("message", "欢迎注册我们的平台！");
        // 用户注册逻辑
        publisher.publishEvent(new CustomMyEvent(objectNode));
        return "注册成功";
    }

    @PostMapping("/async")
    public void async() {
        System.out.println(Thread.currentThread().getName());
        asyncService.asyncMethod();
    }

    @PostMapping("/find")
    public MyTestEntity find() throws JsonProcessingException {
        MyTestEntity build = MyTestEntity.builder().age("18").about("18").signature("18").build();
        System.out.println(mapper.getSerializationConfig().isEnabled(MapperFeature.PROPAGATE_TRANSIENT_MARKER));
        System.out.println(mapper.writeValueAsString(build));
        return build;
    }
}
